package com.example.octopus.service;

import com.alibaba.fastjson2.JSON;
import com.example.octopus.entity.*;
import com.example.octopus.entity.returnMessage.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.*;

@Service
public class ProductServiceImpl implements ProductService{

    private DataBaseService dataBaseService;
    //等待yml注入
    private final String collection="product";
    @Autowired
    public ProductServiceImpl(DataBaseService dataBaseService){
        this.dataBaseService=dataBaseService;
    }
    @Override
    public InsertReturnMessage insert(String env, List<Product> product) {
        return dataBaseService.insert(collection,env,product);
    }
    @Override
    public DeleteReturnMessage delete(String env, Map<String, Object> condition){
        return dataBaseService.delete(collection,env,condition);
    }
    @Override
    public SelectReturnMessage select(String env, Map<String, Object> condition,int page){
        return  dataBaseService.select(collection,env,condition,page);
    }
    @Override
    public SelectReturnMessage select(String env, Map<String, Object> condition){
        return  dataBaseService.select(collection,env,condition);
    }
    @Override
    public UpdateReturnMessage update(String env, String id,Product product){
        Map<String,Object> condition=new HashMap<>();
        condition.put("_id",id);
        SelectReturnMessage selectReturnMessage=dataBaseService.select(collection,env,condition);
        Product product1 = null;
        for(String s: selectReturnMessage.getData()){
           product1= JSON.parseObject(s, Product.class);
        }
        if (product1 != null) {
            return dataBaseService.update(collection,env, product1.get_id(),product);
        }else {
            UpdateReturnMessage updateReturnMessage= new UpdateReturnMessage();
            updateReturnMessage.setErrcode("-1");
            updateReturnMessage.setErrmsg("查找失败");
            return updateReturnMessage;
        }
    }
    @Override
    public SelectReturnMessage selectAll(String env,int page){
            return dataBaseService.select(collection,env,page);
    }
    @Override
    public  List<Product> selectByTitle(String env, String keyword,int page){
        String S="https://api.weixin.qq.com/tcb/invokecloudfunction?access_token="+dataBaseService.getWeixinAccessToken().getAccessToken()+"&env="+"cloud1-6gma1k2s0ff0ac6b"+"&name="+"SelectProductByKeyword";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8"))); // 设置 Accept 字段接受 UTF-8 编码的内容
        int offset=10*page;
        // 发送请求
        String requestBody = "{\"collection\": \"product\", \"env\": \"" + env + "\", \"titleKeyword\": \"" + keyword + "\",\"offset\": "+offset+"}";
        HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(S, entity, String.class);
        System.out.println(requestBody);
        CloudReturnMessage cloudReturnMessage=JSON.parseObject(response.getBody(),CloudReturnMessage.class);
        System.out.println("cloud return"+cloudReturnMessage);
        if (cloudReturnMessage != null) {
            GetCloudMessage getCloudMessage=JSON.parseObject(cloudReturnMessage.getResp_data(),GetCloudMessage.class) ;
            System.out.println(getCloudMessage);
            return getCloudMessage.getData();
        }else {
            return null;
        }
    }
}
